package com.jonluo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jonluo.mapper.SysDeptMapper;
import com.jonluo.model.Example;
import com.jonluo.model.SysDept;
import com.jonluo.service.SysDeptService;
import com.jonluo.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@Service("sysDeptService")
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"deptList", "deptCount","deptAllSubDept","deptSubDept"}, allEntries = true)})
    public int save(SysDept sysDept) {
        return sysDeptMapper.insertSelective(sysDept);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"deptList", "deptCount","deptAllSubDept","deptSubDept"}, allEntries = true)})
    public int saveBatch(List<SysDept> list) {
        int i = 0;
        for (SysDept sysDept : list){
            i += save(sysDept);
        }
        return i;
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"deptList", "deptCount","deptAllSubDept","deptSubDept"}, allEntries = true),
            @CacheEvict(value = "dept", key = "'deptId'+#p0.deptId")})
    public int update(SysDept sysDept) {
        return sysDeptMapper.updateByPrimaryKeySelective(sysDept);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"deptList", "deptCount","deptAllSubDept","deptSubDept"}, allEntries = true),
            @CacheEvict(value = "dept", key = "'deptId'+#p0")})
    public int delete(String id) {
        return sysDeptMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = {"deptList", "deptCount","dept","deptAllSubDept","deptSubDept"}, allEntries = true)})
    public int deleteBatch(String[] ids) {
        Example example = new Example();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("deptId", Arrays.asList(ids));
        return sysDeptMapper.deleteByExample(example);
    }

    @Override
    @Cacheable(value = "dept", key = "'deptId'+#p0")
    public SysDept query(String id) {
        return sysDeptMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "deptList", key = "'deptList'+#p0.toString()")
    public List<SysDept> queryList(PageInfo pageInfo) {
        return sysDeptMapper.selectByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "deptCount", key = "'deptCount'+#p0.toString()")
    public int queryTotal(PageInfo pageInfo) {
        return sysDeptMapper.countByExample(pageInfo2Example(pageInfo));
    }

    @Override
    @Cacheable(value = "deptCount", key = "'deptCount'")
    public int queryTotal() {
        return sysDeptMapper.countByExample(null);
    }


    @Override
    @Cacheable(value = "deptSubDept", key = "'deptSubDept'+#p0")
    public List<SysDept> querySubDeptList(String parentId) {
        Example example = new Example();
        example.setOrderByClause(" order_num asc ");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId);
        criteria.andEqualTo("delFlag", "0");
        return sysDeptMapper.selectByExample(example);
    }

    @Override
    @Cacheable(value = "deptAllSubDept", key = "'deptAllSubDept'+#p0")
    public List<SysDept> queryAllSubDeptList(String parentId) {
        //部门及子部门列表
        List<SysDept> deptList = new ArrayList<>();
        //获取子部门ID
        List<SysDept> subDeptList = querySubDeptList(parentId);
        getDeptTreeList(subDeptList, deptList);
        //添加本部门
        deptList.add(query(parentId));

        return deptList;
    }

    /**
     * 递归擦查询下属部门
     */
    public void getDeptTreeList(List<SysDept> subDeptList,List<SysDept> deptList){
        for(SysDept sysDept : subDeptList){
            List<SysDept> list = querySubDeptList(sysDept.getDeptId());
            if(list.size() > 0){
                getDeptTreeList(list, deptList);
            }
            deptList.add(sysDept);
        }
    }

    /**
     *
     * @param pageInfo
     * 分页参数转为条件封装example
     * @return example
     */
    private  Example pageInfo2Example(PageInfo pageInfo){
        Example example = new Example();
        example.setOrderByClause(" order_num asc ");
        Example.Criteria criteria = example.createCriteria();
        //分页
        if (pageInfo.getOffset()!= 0 &&  pageInfo.getOffset()!= 0){
            example.setOffset(pageInfo.getOffset());
            example.setLimit(pageInfo.getLimit());
        }
        //查询条件
        JSONObject json = pageInfo.getJsonParams();
        /*if (StringUtils.isNotBlank(json.getString("mobile"))){
            criteria.andEqualTo("mobile",json.getString("mobile"));
        }
        if (StringUtils.isNotBlank(json.getString("username"))){
            criteria.andLike("username",json.getString("username"));
        }*/

        return example;
    }
}
